home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / processes / quitapps / quitapps.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  3.3 KB  |  139 lines

  1. /*
  2.     File:        quitapps.c
  3.  
  4.     Contains:    DTS Code Snippet to quit all running applications (except yourself)
  5.     
  6.                 note: to work properly, the calling application must have a standard
  7.                 event loop with menus (to support puppet string quits for apps that
  8.                 don't support core appleevents.
  9.                 
  10.                 note#2: remember to set the applevent aware flag in your app if you use
  11.                 this code
  12.     Written by: Steven Falkenburg     
  13.  
  14.     Copyright:    Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
  15.  
  16.                 You may incorporate this Apple sample source code into your program(s) without
  17.                 restriction. This Apple sample source code has been provided "AS IS" and the
  18.                 responsibility for its operation is yours. You are not permitted to redistribute
  19.                 this Apple sample source code as "Apple sample source code" after having made
  20.                 changes. If you're going to re-distribute the source, we require that you make
  21.                 it clear in the source that the code was descended from Apple sample source
  22.                 code, but that you've made changes.
  23.  
  24.     Change History (most recent first):
  25.                 7/27/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  26.                 
  27.  
  28. */
  29.  
  30. #include <Processes.h>
  31. #include <AppleEvents.h>
  32. #include <Quickdraw.h>
  33. #include <Fonts.h>
  34. #include <Windows.h>
  35. #include <TextEdit.h>
  36. #include <Dialogs.h>
  37.  
  38.  
  39. /* protos */
  40. void QuitAllApps(void);
  41. OSErr QuitAnApp(ProcessSerialNumber *proc);
  42. short GetNumProcs(void);
  43.  
  44. void main()
  45. {    
  46.     InitGraf(&qd.thePort);
  47.     InitFonts();
  48.     InitWindows();
  49.     InitMenus();
  50.     TEInit();
  51.     InitDialogs(nil);
  52.     InitCursor();
  53.     
  54.     
  55.     QuitAllApps();
  56. }
  57. /* this is the entry procedure.  call this to start things going */
  58.  
  59. void QuitAllApps(void)
  60. {
  61.     ProcessSerialNumber ourProc,curProc,*quitApps;
  62.     OSErr err;
  63.     Boolean same;
  64.     short numApps,curAppIndex;
  65.     
  66.     numApps = GetNumProcs() - 1; /* don't count ourselves */
  67.     quitApps = (ProcessSerialNumber *)NewPtr(numApps*sizeof(ProcessSerialNumber));
  68.     if (MemError()!=noErr)
  69.         return;
  70.         
  71.     err = GetCurrentProcess(&ourProc);
  72.     if (err!=noErr)
  73.         return;
  74.     
  75.     curProc.highLongOfPSN = 0;
  76.     curProc.lowLongOfPSN = kNoProcess;
  77.     curAppIndex = 0;
  78.     
  79.     while (GetNextProcess(&curProc)==noErr && curAppIndex<numApps) {
  80.         err = SameProcess(&ourProc,&curProc,&same);
  81.         if (err==noErr && !same) {
  82.             BlockMove(&curProc,&quitApps[curAppIndex++],sizeof(ProcessSerialNumber));
  83.         }
  84.     }
  85.     
  86.     for (curAppIndex=0; curAppIndex<numApps; curAppIndex++)
  87.         QuitAnApp(&quitApps[curAppIndex]);
  88.     
  89.     DisposePtr((Ptr)quitApps);
  90. }
  91.  
  92.  
  93. /* gets the number of current processes */
  94.  
  95. short GetNumProcs(void)
  96. {
  97.     ProcessSerialNumber curProc;
  98.     short numProcs;
  99.     
  100.     numProcs = 0;
  101.     curProc.highLongOfPSN = 0;
  102.     curProc.lowLongOfPSN = kNoProcess;
  103.     
  104.     while (GetNextProcess(&curProc)==noErr)
  105.         numProcs++;
  106.     
  107.     return numProcs;
  108. }
  109.  
  110.  
  111. /* quits an app of the given process id */
  112.  
  113. OSErr QuitAnApp(ProcessSerialNumber *proc)
  114. {
  115.     OSErr err;
  116.     AEAddressDesc target;
  117.     AppleEvent theAE,aeReply;
  118.     
  119.     theAE.dataHandle = aeReply.dataHandle = target.dataHandle = nil;
  120.     
  121.     err = AECreateDesc(typeProcessSerialNumber,(Ptr)proc,sizeof(ProcessSerialNumber),&target);
  122.     if (err!=noErr)
  123.         return err;
  124.  
  125.     err = AECreateAppleEvent(kCoreEventClass,kAEQuitApplication,&target,
  126.                     kAutoGenerateReturnID,kAnyTransactionID,&theAE);
  127.     if (err!=noErr) {
  128.         AEDisposeDesc(&target);
  129.         return err;
  130.     }
  131.      
  132.     err = AESend(&theAE,&aeReply,kAENoReply,kAENormalPriority,kNoTimeOut,nil,nil);
  133.         
  134.     AEDisposeDesc(&target);
  135.     AEDisposeDesc(&theAE);
  136.     
  137.     return err;
  138. }
  139.